package com.example.administrator.testhttpclient;

import android.content.ContentValues;
import android.content.Context;
import android.content.Entity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Message;
import android.os.StrictMode;
import android.preference.PreferenceActivity;
import android.provider.DocumentsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class MainActivity extends AppCompatActivity {
    List<Teacher> teachers ;
    private Spinner spinner;
    private ArrayAdapter<String> adapter;
    MyOpenHelper myOpenHelper;
    SQLiteDatabase db;
    Bitmap bitmap;
    CatchCheckCode catchCheck;
    CatchTeacherInf teacherInf;
    private CatchCourse catchCourse;
    String cookie;
    Button getBitMap;
    public static TextView checkText;
    private static final String[] m={"2015-2016学年第一学期","2014-2015学年第二学期", "2014-2015学年第一学期","2013-2014学年第二学期","2013-2014学年第一学期",
                   "2012-2013学年第二学期","2012-2013学年第一学期", "2011-2012学年第二学期","2011-2012学年第一学期","2010-2011学年第二学期","2010-2011学年第一学期"};

    public static requestInfmation reinf;

       @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           onInit();

       }
      public void onInit() {

          getBitMap = (Button)findViewById(R.id.getBitmap);
          checkText = (TextView)findViewById(R.id.checkCode);
          spinner = (Spinner)findViewById(R.id.spinner);
          adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);
          reinf = new requestInfmation();
          teachers = new ArrayList<Teacher>();
          teacherInf = new CatchTeacherInf();
          catchCheck = new CatchCheckCode();
          catchCourse = new CatchCourse();
          myOpenHelper = new MyOpenHelper(this,"teacher.db",null,1);
          db = myOpenHelper.getWritableDatabase();
          adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);
          adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);
          //设置下拉列表的风格
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          //将adapter 添加到spinner中
          spinner.setAdapter(adapter);
          //设置默认值
          spinner.setVisibility(View.VISIBLE);
          spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
      }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void catchData(View v) {
        teacherInf.catchData();
    }

    public void storeData(View view)  {
       //String sql = "insert into teachers(id, num, tname) values (null, ?, ?)";
        String sql = "insert into teachers(id, num, tname) values (null,?,?)";

        for(Teacher t:teachers ) {
            Object[] par={t.getNum(),t.getTname()};
            Log.i("tiantong", t.getNum());
            Log.i("tiantong",t.getTname());
            db.execSQL(sql, par);
        }
    }


    public void getData(View view) {
        String sql = "select id, num, tname from teachers where id>=?";
        Cursor c = db.rawQuery(sql,new String[]{"1"});
        Teacher t = new Teacher();
        while(c.moveToNext()){
            /*
            int id = c.getInt(0);
            String num = c.getString(1);
            String name = c.getString(2);*/
            t.setTname(c.getString(2));

            Log.i("tiantong",t.getTname());
        }
    }
    public void select(View v) {
        String sql = "select id, num, tname from teachers where id=?";
        Cursor c = db.rawQuery(sql,new String[]{"100"});
        while(c.moveToNext()){
            int id = c.getInt(0);
            String num = c.getString(1);
            String name = c.getString(2);
            Log.i("tiantong",id+".."+num+".."+name);
        }
    }


    public  void catchBi(View v) {

            new Thread() {

                @Override
                public void run() {
                    InputStream in = null;
                    try {
                        in = catchCheck.catchCheckCood("http://121.248.70.214/jwweb/sys/ValidateCode.aspx?t=743");
                        bitmap = BitmapFactory.decodeStream(in);

                        in.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.i("tiantong","1111");
                            ImageView img = (ImageView)findViewById(R.id.img);
                            img.setImageBitmap(bitmap);
                            Log.i("tiantong",reinf.getCookie()+"......");
                        }
                    });


                }
            }.start();
    }

  public void getTable(View v) {

      new Thread(){
          @Override
          public void run() {
              try {
                  String ss = null;
                  ss = catchCourse.getTable();
                  //Log.i("tiantong", ss + "......");
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }.start();

  }
}
